package com.neoCalf.neoChat.service.impl;

import com.neoCalf.neoChat.feigns.ContactServiceFeign;
import com.neoCalf.neoChat.service.ChatService;
import com.neoCalf.neoChat.service.SessionService;
import com.neoCalf.neoCommon.entity.ChatMessage;
import com.neoCalf.neoCommon.entity.ChatSession;
import com.neoCalf.neoCommon.entity.Session;
import com.neoCalf.neoCommon.entity.UserContact;
import com.neoCalf.neoCommon.enums.*;
import com.neoCalf.neoCommon.exception.BusinessException;
import com.neoCalf.neoCommon.model.R;
import com.neoCalf.neoCommon.model.dto.SendMessageDto;
import com.neoCalf.neoCommon.utils.DataTool;
import com.neoCalf.neoCommon.utils.Md5Util;
import com.neoCalf.neoCommon.utils.SendUtils;
import com.neoCalf.neoCommon.utils.SessionSeq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author 小牛
 * @version 1.0
 * @description TODO
 * @date 2025/4/18 10:19
 */
@Service("sessionService")
public class SessionServiceImpl implements SessionService {
    @Autowired
    private ChatService chatService;
    @Autowired
    private SendUtils sendUtils;
    @Autowired
    private ContactServiceFeign contactServiceFeign;
    @Autowired
    private SessionSeq sessionSeq;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ChatMessage sendText(SendMessageDto<Object> sendMessageDto) {
        UserContactTypeEnum contactTypeEnum = DataTool.getContactTypeEnum(sendMessageDto.getContactId());
        int allow = UserContactStatusEnum.FRIEND.getStatus();
        R<UserContact> userContactR = contactServiceFeign.findUserContact(sendMessageDto.getSendUserId(), sendMessageDto.getContactId());
        if (userContactR.getResult() == null) {
            allow = UserContactStatusEnum.DEL.getStatus();
        }
        R<UserContact> userContactR2 = contactServiceFeign.findUserContact(sendMessageDto.getContactId(), sendMessageDto.getSendUserId());
        if (userContactR2.getResult() == null) {
            allow = UserContactStatusEnum.DEL.getStatus();
        }else {
            if (userContactR2.getResult().getStatus() == UserContactStatusEnum.BLACKLIST.getStatus()) {
                allow = UserContactStatusEnum.BLACKLIST.getStatus();
            }
        }
        String sessionId = sendMessageDto.getSessionId();
        ChatSession chatSession = new ChatSession(sessionId, sendMessageDto.getLastMessage(), sendMessageDto.getSendTime());
        ChatMessage chatMessage = new ChatMessage(
                sessionSeq.next(sessionId),
                sessionId,
                MessageTypeEnum.CHAT.getType(),
                sendMessageDto.getMessageContent(),
                sendMessageDto.getSendUserId(),
                sendMessageDto.getSendUserNickName(),
                sendMessageDto.getSendTime(),
                sendMessageDto.getContactId(),
                contactTypeEnum.getType(),
                0
        );
        if (allow == UserContactStatusEnum.FRIEND.getStatus()) {
            chatMessage.setStatus(MessageStatusEnum.FLUSH.getStatus());
            sendMessageDto.setMessageId(chatMessage.getMessageId());
            sendMessageDto.setMessageType(MessageTypeEnum.CHAT.getType());
            sendMessageDto.setContactType(UserContactTypeEnum.USER.getType());
            sendMessageDto.setStatus(chatMessage.getStatus());
            sendUtils.sendMessage(sendMessageDto);
            chatService.updateChatSession(chatSession);
            chatService.addChatMessage(chatMessage);
        } else {
            chatMessage.setStatus(MessageStatusEnum.FAILED.getStatus());
            chatMessage.setFailReason(allow);
            SendMessageDto<ChatMessage> sendMessageDto2 = new SendMessageDto<>();
            ChatMessage chatMessage2 = new ChatMessage(
                    sessionSeq.next(sessionId),
                    sessionId,
                    MessageTypeEnum.NOT_FRIEND.getType(),
                    MessageTypeEnum.NOT_FRIEND.getInitMessage(),
                    sendMessageDto.getContactId(),
                    sendMessageDto.getContactName(),
                    sendMessageDto.getSendTime(),
                    sendMessageDto.getSendUserId(),
                    contactTypeEnum.getType(),
                    MessageStatusEnum.FLUSH.getStatus()
            );
            sendMessageDto2.setMessageType(chatMessage2.getMessageType());
            sendMessageDto2.setContactId(sendMessageDto.getSendUserId());
            sendMessageDto2.setContactType(contactTypeEnum.getType());
            sendMessageDto2.setExtendData(chatMessage2);
            sendUtils.sendMessage(sendMessageDto2);
        }
        return chatMessage;
    }

    @Override
    public ChatMessage sendMedia(SendMessageDto<MultipartFile> sendMessageDto) {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ChatMessage sendTextToRobot(SendMessageDto<Object> sendMessageDto) {
        UserContactTypeEnum contactTypeEnum = DataTool.getContactTypeEnum(sendMessageDto.getContactId());
        if (!contactTypeEnum.equals(UserContactTypeEnum.ROBOT)) {
            throw new BusinessException(ResultCode.CODE_10017, "联系人类型错误");
        }
        String sessionId = sendMessageDto.getSessionId();
        ChatSession chatSession = new ChatSession(sessionId, sendMessageDto.getLastMessage(), sendMessageDto.getSendTime());
        ChatMessage chatMessage = new ChatMessage(
                sessionSeq.next(sessionId),
                sessionId,
                MessageTypeEnum.CHAT.getType(),
                sendMessageDto.getMessageContent(),
                sendMessageDto.getSendUserId(),
                sendMessageDto.getSendUserNickName(),
                sendMessageDto.getSendTime(),
                sendMessageDto.getContactId(),
                contactTypeEnum.getType(),
                MessageStatusEnum.FLUSH.getStatus()
        );
        chatService.updateChatSession(chatSession);
        chatService.addChatMessage(chatMessage);

        chatMessage.setStatus(MessageStatusEnum.FLUSH.getStatus());
        sendMessageDto.setContactType(UserContactTypeEnum.ROBOT.getType());
        // TODO 加入机器人回答队列
        return chatMessage;
    }
}
